import router from '@/router';

class Common {
  /* 导出excel信息 */
  exportExcel (data, _name) {
    const blob = new Blob([data.data], {
      type: 'application/vnd.ms-excel'
    });
    const fileName = _name;
    const elink = document.createElement('a');
    elink.download = fileName;
    elink.style.display = 'none';
    /* 将blob对象转成url地址 */
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  }

  /* 格林尼治时间转date */
  renderTime (date) {
    if (date === '') {
      return '';
    } else {
      var _date = new Date(date).toJSON();
      return new Date(+new Date(_date) + 8 * 3600 * 1000)
        .toISOString()
        .replace(/T/g, ' ')
        .replace(/\.[\d]{3}Z/, '');
    }
  }

  /* 获取年月日 时分秒 yyyy-MM-dd hh:mm:ss*/
  dateFormatYMDHMS (timestamp) {
    let date = new Date(timestamp);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) + '';
    return Y + M + D + h + m + s;
  }

  /* 获取 时分秒 hh:mm:ss*/
  dateFormatHMS (timestamp) {
    let date = new Date(timestamp);
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) + '';
    return h + m + s;
  }

  /**
   * 获取今天某点时间
   * @param {*} hours 某小时
   * @param {*} minutes 某分
   * @returns 时间
   */
  anyDate (hours, minutes = 0) {
    return new Date(
      new Date(new Date().getTime()).setHours(hours, minutes, 0)
    )
  }

  /**
   * 跳转到空白新页面
   * @param {*} path 路径
   * @param {*} query 请求参数，是个对象
   */
  toBlankPage (path, query) {
    let routeData = router.resolve({
      path,
      query
    });
    window.open(routeData.href, '_blank');
  }

  /* 退出登录 */
  signOut (data) {
    localStorage.removeItem('token');
    router.push(data);
  }

  /* 简单校验手机号 */
  isPhone (phone) {
    if (phone === '' || phone === null || phone === undefined) return false;
    return /^1(3\d|4\d|5\d|6\d|7\d|8\d|9\d)\d{8}$/g.test(phone);
  }

  /**
   * @param {Object} idList 要删除的id列表
   * @param {Object} list 在哪个列表删除
   * @param {Object} id 以哪个字段进行删除
   * 删除选中的数据
   */
  handleSelectDeleteData (idList, list, id = 'id') {
    for (let i = 0; i < idList.length; i++) {
      for (let j = 0; j < list.length; j++) {
        if (idList[i] == list[j][id]) {
          list.splice(j, 1);
          break;
        }
      }
    }
  }
}
export const common = new Common();